package jp.ac.u_tokyo.dch.dao.data;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import jp.ac.u_tokyo.dch.core.db.CustomPreparedStatement;
import jp.ac.u_tokyo.dch.core.db.CustomResultSet;
import jp.ac.u_tokyo.dch.core.db.DBConnection;
import jp.ac.u_tokyo.dch.core.exceptions.DBException;
import jp.ac.u_tokyo.dch.model.DataGroup;
import jp.ac.u_tokyo.dch.model.db.Data;
import jp.ac.u_tokyo.dch.model.xml.tree.Rdata;
import jp.ac.u_tokyo.dch.model.xml.tree.TreeData;
import jp.ac.u_tokyo.dch.util.DchCommonUtil;
import jp.ac.u_tokyo.dch.util.common.CommonUtil;
import jp.ac.u_tokyo.dch.util.common.CommonDBConnection;
import jp.ac.u_tokyo.dch.view.Constants;

import org.apache.commons.lang.xwork.StringUtils;
/**
* @version 1.1
*/
public class DataDao {

	/**
	 * DBコネクションオブジェクト
	 */
	private DBConnection dbconn = null;

	/**
	 * 生成者
	 * @param DBコネクションオブジェクト
	 */
	public DataDao(DBConnection dbconn){
		this.dbconn = dbconn;
	}



	/**
	 * データを取得する
	 * @param fieldKey 項目キー
	 * @param datacontent データ
	 * @return ツリー項目リスト
	 * @throws DBException 例外
	 */
	public List<Rdata> selectSearchByFieldKey(String fieldKey, String datacontent) throws DBException {

		 List<Rdata> dataList = new ArrayList<Rdata>();

		StringBuilder sqlQuery = new StringBuilder();
		sqlQuery.append("SELECT l6name,total_data_id FROM  ");
		sqlQuery.append("total_data INNER JOIN ");
		sqlQuery.append("total_data_items INNER JOIN ");
		sqlQuery.append("data on total_data_items.dataid = data.id and total_data.l6id = total_data_items.total_data_id ");
		sqlQuery.append("WHERE fieldkey = ? AND datacontent = ? ");

		CustomPreparedStatement ps = null;
		CustomResultSet rs = null;
		try{
			ps = dbconn.prepareStatement(sqlQuery.toString());
			ps.setString(1,  fieldKey);
			ps.setString(2,  datacontent);
			rs = ps.executeQuery();

			while (rs.next()) {

				Rdata data = new Rdata();
				data.setLabel(rs.getString("l6name"));
				data.setValue(rs.getString("total_data_id"));
				dataList.add(data);
			}

		}catch(DBException dbe){
			throw dbe;
		}finally{
			CommonDBConnection.closeResource(ps, rs);
		}
		return dataList;

	}

	/**
	 * すべてのデータを取得する
	 * @return データリスト
	 * @throws DBException 例外
	 */
	public List<List<TreeData>> selectSearchAll() throws DBException {

		List<List<TreeData>> list = new ArrayList<List<TreeData>>();

		StringBuilder sqlQuery = new StringBuilder();
		sqlQuery.append("select l6id,name from total_data inner join data on total_data.l6id = data.id ");

		CustomPreparedStatement ps = null;
		CustomResultSet rs = null;
		try{
			ps = dbconn.prepareStatement(sqlQuery.toString());
			rs = ps.executeQuery();

			while (rs.next()) {

				List<TreeData> dataList = new ArrayList<TreeData>();
				TreeData treeData = new TreeData(rs.getString("NAME"), rs.getString("L6ID"));
				dataList.add(treeData);
				list.add(dataList);
			}

		}catch(DBException dbe){
			throw dbe;
		}finally{
			CommonDBConnection.closeResource(ps, rs);
		}
		return list;

	}

	/**
	 * データを取得する
	 * @param データID
	 * @return データオブジェクト
	 * @throws DBException 例外
	 */
	public Data selectById(String id) throws DBException {

		Data data = null;

		StringBuilder sqlQuery = new StringBuilder();
		sqlQuery.append("SELECT child.id, child.schemaid, child.oyaid,child.name,child.sort,child.createuser,child.createdate,child.modifyuser,child.modifydate,oya.name as oyaname ");
		sqlQuery.append("FROM ");
		sqlQuery.append("data as child LEFT OUTER JOIN data oya on child.oyaid = oya.id ");
		sqlQuery.append("WHERE child.id = ? ");

		CustomPreparedStatement ps = null;
		CustomResultSet rs = null;
		try{
			ps = dbconn.prepareStatement(sqlQuery.toString());
			ps.setInt(1,  Integer.valueOf(id));
			rs = ps.executeQuery();

			if (rs.next()) {

				data = new Data();

				data.setId(id);
				data.setOyaId(rs.getString("oyaid"));
				data.setOyaName(rs.getString("oyaname"));
				data.setName(rs.getString("name"));
				data.setSchemaId(rs.getString("schemaid"));
				data.setSort(rs.getString("sort"));
				data.setCreateUser(rs.getString("createuser"));
				data.setCreateDate(rs.getString("createdate"));
				data.setModifyUser(rs.getString("modifyuser"));
				data.setModifyDate(rs.getString("modifydate"));
			}

		}catch(DBException dbe){
			throw dbe;
		}finally{
			CommonDBConnection.closeResource(ps, rs);
		}
		return data;

	}

	/**
	 * データを挿入する
	 * @param data データ
	 * @return 挿入件数
	 * @throws DBException 例外
	 */
	public int insert(Data data) throws DBException {

		CustomPreparedStatement ps = null;
		int result = 0;
		int index = 1;
		try{
			String query = "INSERT INTO data (id, schemaId,oyaId,name,sort,createuser,createdate,modifyuser,modifydate) VALUES (?,?,?,?,?,?,?,?,?)";

			ps = dbconn.prepareStatement(query);
			ps.setInt(index++, Integer.valueOf(data.getId()));
			ps.setString(index++, data.getSchemaId());
			ps.setString(index++, data.getOyaId());
			ps.setString(index++, data.getName());
			ps.setString(index++, data.getSort());
			ps.setString(index++, data.getCreateUser());
			ps.setString(index++, data.getCreateDate());
			ps.setString(index++, data.getModifyUser());
			ps.setString(index++, data.getModifyDate());
			result = ps.executeUpdate();
		}catch(DBException dbe){
			throw dbe;
		}finally{
			CommonDBConnection.closeResource(ps);
		}
		return result;
	}

	/**
	 * 更新する
	 * @param data データ
	 * @return 更新件数
	 * @throws DBException 例外
	 */
	public int update(Data data) throws DBException {

		CustomPreparedStatement ps = null;
		int result = 0;
		int index = 1;
		try{
			String query = "UPDATE data SET schemaId = ?,oyaId = ?,name = ?,sort = ?,createuser = ?,createdate = ?,modifyuser = ?,modifydate = ? WHERE id = ?";

			ps = dbconn.prepareStatement(query);
			ps.setString(index++, data.getSchemaId());
			ps.setString(index++, data.getOyaId());
			ps.setString(index++, data.getName());
			ps.setString(index++, data.getSort());
			ps.setString(index++, data.getCreateUser());
			ps.setString(index++, data.getCreateDate());
			ps.setString(index++, data.getModifyUser());
			ps.setString(index++, data.getModifyDate());
			ps.setInt(index++, Integer.valueOf(data.getId()));
			result = ps.executeUpdate();
		}catch(DBException dbe){
			throw dbe;
		}finally{
			CommonDBConnection.closeResource(ps);
		}
		return result;
	}


	/**
	 * 削除する
	 * @param data データ
	 * @return 削除件数
	 * @throws DBException 例外
	 */
	public int delete(Data data) throws DBException {

		CustomPreparedStatement ps = null;
		int result = 0;
		int index = 1;
		try{
			String query = "DELETE FROM data WHERE id = ?";

			ps = dbconn.prepareStatement(query);
			ps.setInt(index++, Integer.valueOf(data.getId()));
			result = ps.executeUpdate();
		}catch(DBException dbe){
			throw dbe;
		}finally{
			CommonDBConnection.closeResource(ps);
		}
		return result;


	}

	/**
	 * データ検索
	 * @param searchKeyMap 検索項目
	 * @return 検索件数
	 * @throws DBException 例外
	 */
	public int getSearchResultCnt(Map<String, String> searchKeyMap) throws DBException {



		StringBuilder sqlQuery = new StringBuilder();
		//count(distinct DATA.ID)
		//sqlQuery.append("SELECT DATA.ID, DATA.sort,DATA_SCHEMA.name,DATA.name,DATA.createuser,DATA.createdate,DATA.modifyuser,DATA.modifydate FROM");
		sqlQuery.append("SELECT count(*) as count FROM data as totaldata ");

		Map<String, String> keywordList = makeKeyList(searchKeyMap);

		if (keywordList.containsKey("keyword")){
			sqlQuery.append(keywordList.get("keyword"));
		}

		List<String> whereList = new ArrayList<String>();

		if (keywordList.containsKey("dataType")){

			whereList.add(keywordList.get("dataType"));
		}
		if (keywordList.containsKey("schemaid")){
			whereList.add(keywordList.get("schemaid"));
		}
		if (keywordList.containsKey("popupSign")){
			whereList.add(keywordList.get("popupSign"));
		}

		if (!whereList.isEmpty()) {
			sqlQuery.append(" where ");
			sqlQuery.append(StringUtils.join(whereList, " and "));
		}

		CustomPreparedStatement ps = null;
		CustomResultSet rs = null;
		int result = 0;
		try{
			ps = dbconn.prepareStatement(sqlQuery.toString());

			setSearchValues(searchKeyMap, keywordList, ps);

			rs = ps.executeQuery();

			if (rs.next()) {

				result = rs.getInt("count");
			}

		}catch(DBException dbe){
			throw dbe;
		}finally{
			CommonDBConnection.closeResource(ps, rs);
		}
		return result;
	}

	/**
	 * 検索して結果を返す
	 * @param start 開始インデックス
	 * @param end 終了インデックス
	 * @param searchKeyMap 検索項目
	 * @return 検索結果
	 * @throws Exception 例外
	 */
	public List<Data> getSearchResult(int start, int end, Map<String, String> searchKeyMap) throws Exception {

		List<Data> list = new ArrayList<Data>();

		String limit = "limit %d, %d";

		StringBuilder sqlQuery = new StringBuilder();
		sqlQuery.append("SELECT * FROM data as totaldata INNER JOIN data_schema on totaldata.schemaid = data_schema.schemaid ");

		Map<String, String> keywordList = makeKeyList(searchKeyMap);

		if (keywordList.containsKey("keyword")){
			sqlQuery.append(keywordList.get("keyword"));
		}

		List<String> whereList = new ArrayList<String>();

		if (keywordList.containsKey("dataType")){

			whereList.add(keywordList.get("dataType"));
		}
		if (keywordList.containsKey("schemaid")){
			whereList.add(keywordList.get("schemaid"));
		}
		if (keywordList.containsKey("popupSign")){
			whereList.add(keywordList.get("popupSign"));
		}

		if (!whereList.isEmpty()) {
			sqlQuery.append(" where ");
			sqlQuery.append(StringUtils.join(whereList, " and "));
		}
		sqlQuery.append(" order by totaldata.id desc ");
		sqlQuery.append(String.format(limit, start, end));

		CustomPreparedStatement ps = null;
		CustomResultSet rs = null;
		try{
			ps = dbconn.prepareStatement(sqlQuery.toString());

			setSearchValues(searchKeyMap, keywordList, ps);

			rs = ps.executeQuery();

			while (rs.next()) {

				Data data = new Data();

				data.setId(rs.getString("id"));
				data.setSort(rs.getString("totaldata.sort"));
				data.setSchemaId(rs.getString("data_schema.schemaid"));
				data.setSchemaName(rs.getString("data_schema.name"));
				data.setName(rs.getString("name"));
				data.setCreateUser(rs.getString("totaldata.createuser"));
				data.setCreateDate(rs.getString("totaldata.createdate"));
				data.setModifyUser(rs.getString("totaldata.modifyuser"));
				data.setModifyDate(rs.getString("totaldata.modifydate"));

				list.add(data);
			}

		}catch(DBException dbe){
			throw dbe;
		}finally{
			CommonDBConnection.closeResource(ps, rs);
		}
		return list;

	}

	/**
	 *
	 * @param id
	 * @return
	 * @throws DBException
	 */
	public List<Data> selectChildData(String oyaid, String sort, String schemaid) throws DBException {

		List<Data> list = new ArrayList<Data>();


		StringBuilder sqlQuery = new StringBuilder();
		sqlQuery.append("SELECT * FROM data WHERE oyaid = ? and sort = ? and schemaid = ?");

		CustomPreparedStatement ps = null;
		CustomResultSet rs = null;
		try{
			ps = dbconn.prepareStatement(sqlQuery.toString());

			ps.setInt(1, Integer.valueOf(oyaid));
			ps.setInt(2, Integer.valueOf(sort));
			ps.setInt(3, Integer.valueOf(schemaid));

			rs = ps.executeQuery();

			while (rs.next()) {

				Data data = new Data();

				data.setId(rs.getString("id"));
				data.setOyaId(rs.getString("oyaid"));
				data.setName(rs.getString("name"));
				data.setSchemaId(rs.getString("schemaid"));
				data.setSort(rs.getString("sort"));
				data.setCreateUser(rs.getString("createuser"));
				data.setCreateDate(rs.getString("createdate"));
				data.setModifyUser(rs.getString("modifyuser"));
				data.setModifyDate(rs.getString("modifydate"));

				list.add(data);
			}

		}catch(DBException dbe){
			throw dbe;
		}finally{
			CommonDBConnection.closeResource(ps, rs);
		}
		return list;
	}

	/**
	 *
	 * @param id
	 * @return
	 * @throws DBException
	 */
	public List<Data> selectLv5Data(String lv3id, String lv5Schemaid) throws DBException {

		List<Data> list = new ArrayList<Data>();


		StringBuilder sqlQuery = new StringBuilder();
		sqlQuery.append("SELECT * FROM data L3 INNER JOIN data L4  ");
		sqlQuery.append("INNER JOIN data L5 on L3.sort = '003' AND L3.id = L4.oyaid  AND  L4.id = L5.oyaid");
		sqlQuery.append(" WHERE L3.id = ? AND L5.schemaid = ?");

		CustomPreparedStatement ps = null;
		CustomResultSet rs = null;
		try{
			ps = dbconn.prepareStatement(sqlQuery.toString());

			ps.setInt(1, Integer.valueOf(lv3id));
			ps.setInt(2, Integer.valueOf(lv5Schemaid));

			rs = ps.executeQuery();

			while (rs.next()) {

				Data data = new Data();

				data.setId(rs.getString("id"));
				data.setOyaId(rs.getString("oyaid"));
				data.setName(rs.getString("name"));
				data.setSchemaId(rs.getString("schemaid"));
				data.setSort(rs.getString("sort"));
				data.setCreateUser(rs.getString("createuser"));
				data.setCreateDate(rs.getString("createdate"));
				data.setModifyUser(rs.getString("modifyuser"));
				data.setModifyDate(rs.getString("modifydate"));

				list.add(data);
			}

		}catch(DBException dbe){
			throw dbe;
		}finally{
			CommonDBConnection.closeResource(ps, rs);
		}
		return list;
	}

	/**
	 *
	 * @param id
	 * @return
	 * @throws DBException
	 */
	public List<Data> selectLv6Data(String lv3id, String lv6Schemaid) throws DBException {

		List<Data> list = new ArrayList<Data>();


		StringBuilder sqlQuery = new StringBuilder();
		sqlQuery.append("SELECT * FROM data L3 INNER JOIN data L4  ");
		sqlQuery.append("INNER JOIN data L5 INNER JOIN data L6 on L3.sort = '003' AND L3.id = L4.oyaid  AND  L4.id = L5.oyaid AND  L5.id = L6.oyaid ");
		sqlQuery.append(" WHERE L3.id = ? AND L6.schemaid = ?");

		CustomPreparedStatement ps = null;
		CustomResultSet rs = null;
		try{
			ps = dbconn.prepareStatement(sqlQuery.toString());

			ps.setInt(1, Integer.valueOf(lv3id));
			ps.setInt(2, Integer.valueOf(lv6Schemaid));

			rs = ps.executeQuery();

			while (rs.next()) {

				Data data = new Data();

				data.setId(rs.getString("id"));
				data.setOyaId(rs.getString("oyaid"));
				data.setName(rs.getString("name"));
				data.setSchemaId(rs.getString("schemaid"));
				data.setSort(rs.getString("sort"));
				data.setCreateUser(rs.getString("createuser"));
				data.setCreateDate(rs.getString("createdate"));
				data.setModifyUser(rs.getString("modifyuser"));
				data.setModifyDate(rs.getString("modifydate"));

				list.add(data);
			}

		}catch(DBException dbe){
			throw dbe;
		}finally{
			CommonDBConnection.closeResource(ps, rs);
		}
		return list;
	}

	public List<DataGroup> selectDataGroupByL3(String l6Schemaid) throws DBException {

		List<DataGroup> list = new ArrayList<DataGroup>();


		StringBuilder sqlQuery = new StringBuilder();
		sqlQuery.append("SELECT * FROM total_data WHERE l6Schemaid = ? group by  l3id ");
		CustomPreparedStatement ps = null;
		CustomResultSet rs = null;
		try{
			ps = dbconn.prepareStatement(sqlQuery.toString());

			ps.setInt(1, Integer.valueOf(l6Schemaid));

			rs = ps.executeQuery();

			while (rs.next()) {

				DataGroup data = new DataGroup();

				data.setLv1Dataid(rs.getString("L1ID"));
				data.setLv1DataName(rs.getString("L1NAME"));
				data.setLv1Schemaid(rs.getString("L1SCHEMAID"));

				data.setLv2Dataid(rs.getString("L2ID"));
				data.setLv2DataName(rs.getString("L2NAME"));
				data.setLv2Schemaid(rs.getString("L2SCHEMAID"));

				data.setLv3Dataid(rs.getString("L3ID"));
				data.setLv3DataName(rs.getString("L3NAME"));
				data.setLv3Schemaid(rs.getString("L3SCHEMAID"));

				list.add(data);
			}

		}catch(DBException dbe){
			throw dbe;
		}finally{
			CommonDBConnection.closeResource(ps, rs);
		}
		return list;
	}

	/**
	 * データ名をチェックする
	 * @param name 名前
	 * @param id データID
	 * @return true：重複存在、false：存在しない
	 * @throws DBException
	 */
	public boolean checkName(String sort, String name, String id)throws DBException {

		CustomPreparedStatement pstmt = null;
		CustomResultSet rs = null;
		String sql = "";
		boolean state = false;
		try{
			if(StringUtils.isNotEmpty(name)){
				if(StringUtils.isNotEmpty(id)){
					sql = "select name from data where sort = ? and name = ? and id != ?";
					pstmt = dbconn.prepareStatement(sql);
					pstmt.setString(1, sort);
					pstmt.setString(2, name);
					pstmt.setString(3, id);
				}else{
					sql = "select name from data where sort = ? and name = ?";
					pstmt = dbconn.prepareStatement(sql);
					pstmt.setString(1, sort);
					pstmt.setString(2, name);
				}
			}
			rs = pstmt.executeQuery();

			if (rs.next()) {
				state = true;
			} else {
				state = false;
			}
		}catch(DBException e) {
			throw e;
		} finally{
			CommonDBConnection.closeResource(pstmt, rs);

		}
		return state;
	}

	/**
	 * データ検索
	 * @param searchKeyMap 検索項目
	 * @return 検索件数
	 * @throws DBException 例外
	 */
	public int getOyaSearchResultCnt(Map<String, String> searchKeyMap) throws DBException {



		StringBuilder sqlQuery = new StringBuilder();
		//count(distinct DATA.ID)
		//sqlQuery.append("SELECT DATA.ID, DATA.sort,DATA_SCHEMA.name,DATA.name,DATA.createuser,DATA.createdate,DATA.modifyuser,DATA.modifydate FROM");
		sqlQuery.append("SELECT count(*) as count FROM data INNER JOIN (select distinct oyaid FROM data WHERE schemaid = ? and sort = ?) as childdata on data.id = childdata.oyaid ");

		String sDataType = DchCommonUtil.trimUni(searchKeyMap.get("dataType"));
		String schemaid = DchCommonUtil.trimUni(searchKeyMap.get("schemaid"));



		CustomPreparedStatement ps = null;
		CustomResultSet rs = null;
		int result = 0;
		try{
			ps = dbconn.prepareStatement(sqlQuery.toString());
			ps.setString(1, schemaid);
			ps.setString(2, sDataType);

			rs = ps.executeQuery();

			if (rs.next()) {

				result = rs.getInt("count");
			}

		}catch(DBException dbe){
			throw dbe;
		}finally{
			CommonDBConnection.closeResource(ps, rs);
		}
		return result;
	}

	/**
	 * 検索して結果を返す
	 * @param start 開始インデックス
	 * @param end 終了インデックス
	 * @param searchKeyMap 検索項目
	 * @return 検索結果
	 * @throws Exception 例外
	 */
	public List<Data> getOyaSearchResult(int start, int end, Map<String, String> searchKeyMap) throws Exception {

		List<Data> list = new ArrayList<Data>();

		String limit = "limit %d, %d";

		StringBuilder sqlQuery = new StringBuilder();
		sqlQuery.append("SELECT * FROM data INNER JOIN (select distinct oyaid FROM data WHERE schemaid = ? and sort = ?) as childdata on data.id = childdata.oyaid  ");

		String schemaid = DchCommonUtil.trimUni(searchKeyMap.get("schemaid"));
		String sDataType = DchCommonUtil.trimUni(searchKeyMap.get("dataType"));

		sqlQuery.append(" order by data.name desc ");
		sqlQuery.append(String.format(limit, start, end));

		CustomPreparedStatement ps = null;
		CustomResultSet rs = null;
		try{
			ps = dbconn.prepareStatement(sqlQuery.toString());
			ps.setString(1, schemaid);
			ps.setString(2, sDataType);
			rs = ps.executeQuery();

			while (rs.next()) {

				Data data = new Data();

				data.setId(rs.getString("id"));
				data.setSort(rs.getString("data.sort"));
				data.setSchemaId(rs.getString("data.schemaid"));
				data.setName(rs.getString("data.name"));
				data.setCreateUser(rs.getString("data.createuser"));
				data.setCreateDate(rs.getString("data.createdate"));
				data.setModifyUser(rs.getString("data.modifyuser"));
				data.setModifyDate(rs.getString("data.modifydate"));

				list.add(data);
			}

		}catch(DBException dbe){
			throw dbe;
		}finally{
			CommonDBConnection.closeResource(ps, rs);
		}
		return list;
	}

	/**
	 * 検索条件をリストに変える
	 * @param searchKeyMap 検索項目
	 * @return 検索条件リスト
	 */
	private Map<String, String> makeKeyList(Map<String,String> searchKeyMap) {

		//List<String> keywordList = new ArrayList<String>();

		Map<String, String> map = new LinkedHashMap<String, String>();

		String sDataType = DchCommonUtil.trimUni(searchKeyMap.get("dataType"));
		String schemaid = DchCommonUtil.trimUni(searchKeyMap.get("schemaid"));
		String sKeyword = DchCommonUtil.trimUni(searchKeyMap.get("keyword"));
		String popupSign = DchCommonUtil.trimUni(searchKeyMap.get("popupSign"));

		if(StringUtils.isNotEmpty(sDataType)){
			map.put("dataType", "totaldata.sort = ?");
		}
		if(StringUtils.isNotEmpty(schemaid)){

			map.put("schemaid", "totaldata.schemaid = ?");
		}
		if(StringUtils.isNotEmpty(sKeyword)){

			StringBuilder search = new StringBuilder();

			search.append(" INNER JOIN ");
			search.append("(SELECT id as dataid FROM data where name like ? OR createuser like ? ");
			search.append(" UNION ");
			search.append(" SELECT dataid FROM  data_items  where  data_items.data like ?  OR  data_items.textareadata like ? ");
			search.append(" UNION  ");
			search.append(" SELECT dataid FROM data_file where data_file.uploadfilename like ? or data_file.filename like ?) as uniondata ");
			search.append(" on totaldata.id = uniondata.dataid  ");

			map.put("keyword", search.toString());
		}

		if(StringUtils.isNotEmpty(popupSign) && "1".equals(popupSign) && Constants.LV6_SORT.equals(sDataType)){

			map.put("popupSign", "totaldata.sort != '007'");
		}


		return map;
	}

	private void setSearchValues(Map<String, String> searchKeyMap, Map<String, String> keywordList, CustomPreparedStatement ps) throws DBException {

		int i = 1;

		if (searchKeyMap.containsKey("keyword")) {

			if(StringUtils.isNotEmpty(searchKeyMap.get("keyword"))){
				String search = CommonUtil.escapeLike(searchKeyMap.get("keyword"));
				ps.setString(i++, "%" + search + "%");
				ps.setString(i++, "%" + search + "%");
				ps.setString(i++, "%" + search + "%");
				ps.setString(i++, "%" + search + "%");
				ps.setString(i++, "%" + search + "%");
				ps.setString(i++, "%" + search + "%");
			}
		}

		if (searchKeyMap.containsKey("dataType")) {

			if(StringUtils.isNotEmpty(searchKeyMap.get("dataType"))){

				ps.setString(i++, searchKeyMap.get("dataType"));
			}
		}

		if (searchKeyMap.containsKey("schemaid")) {

			if(StringUtils.isNotEmpty(searchKeyMap.get("schemaid"))){

				ps.setString(i++, searchKeyMap.get("schemaid"));
			}
		}

	}


}
